.org 0xE7BE8 ; Movement Cost

Movement_Cost:

	PUSH 	{R4-R11, LR}	
	SUB		SP, SP, 0x1C
	
	MOV		R4, 1

Movement_Cost_BLT_3:

	AND		R0, R4, 0xFF
	
	BL		0xC237C	
	
	CMP		R0, 0
	
	BEQ		Movement_Cost_Break_1
	
	MOV		R0, R4	
	
	BL		0xE70B8

Movement_Cost_Break_1:	

	ADD 	R4, R4, 1
	CMP 	R4, 5
	
	BLT		Movement_Cost_BLT_3
	
	MOV		R10, 1
	MOV		R8, 0x98		
	
	LDR 	R6, [Movement_Cost_1]
	LDR 	R5, [Movement_Cost_2]
	LDR 	R4, [Movement_Cost_3]
	
	STR		R10, [SP, 0xC]
	STR		R10, [SP, 0x14]
	STR		R8, [SP, 0x18]
	
	MOV 	R0, Rank_Pathfinder					; Rank: Pathfinder	
	
	STR		R0, [SP, 0x4]
	
	MOV		R11, 0								; Default Movement Cost / Temporary Terrain Defence
	
	MOV 	R0, Rank_Prairie_Dog				; Rank: Prairie Dog	
	
	STR		R0, [SP, 0x0]
	MOV		R0, 2
	STR		R0, [SP, 0x10]
	MOV		R0, 3
	STR		R0, [SP, 0x8]

Movement_Cost_BLT_2:

	AND		R0, R10, 0xFF
	
	BL		0xC237C	
	
	CMP		R0, 0
	
	BEQ		Movement_Cost_Break_2

;-------------------------------------------------------------------------------
	
	LDR 	R2, [R5, 0]
	LDR 	R0, [R4, 0]
	LDR 	R1, [SP, 0]
	
	ADD 	R0, R2, R0, LSL 2
	LDR 	R0, [R0, 0x48]
	ADD 	R0, R0, 0x5000	
	LDR 	R0, [R0, 0xA58]
	ADD		R0, R0, R8
	
	STRB	R11, [R0, 0x2B]						; Default Movement Cost
	
;-------------------------------------------------------------------------------
	
	LDR		R2, [R5, 0x0]
	LDR		R0, [R4, 0x0]
	ADD		R0, R2, R0, LSL 2
	LDR 	R0, [R0, 0x48]
	ADD 	R0, R0, 0x5000	
	LDR 	R0, [R0, 0xA58]
	ADD		R0, R0, R8
	ADD		R0, R0, 0x74
	
	BL 		Check_Skills_Equipped				; Check the Skill is equipped
	CMP 	R0, 0								; Does CO use Prairie Dog?
	BEQ		No_Skill_Prairie_Dog				; Branch if CO DOES NOT use Prairie Dog	
	
	LDR 	R2, [R5, 0]
	LDR 	R0, [R4, 0]
	LDR 	R1, [SP, 4]
	
	ADD 	R0, R2, R0, LSL 2
	LDR 	R0, [R0, 0x48]
	ADD 	R0, R0, 0x5000	
	LDR 	R0, [R0, 0xA58]
	ADD		R0, R0, R8
	ADD		R0, R0, 0x74
	
	BL 		Check_Skills_Equipped				; Check the Skill is equipped
	CMP 	R0, 0								; Does CO use Pathfinder?
	BEQ		No_Skill_Pathfinder					; Branch if CO DOES NOT use Pathfinder
	
	LDR 	R1, [R5, 0]
	LDR 	R0, [R4, 0]
	ADD 	R0, R1, R0, LSL 2
	LDR 	R0, [R0, 0x48]
	ADD 	R0, R0, 0x5000	
	LDR 	R0, [R0, 0xA58]
	ADD		R1, R0, R8
	LDR		R0, [SP, 0x8]
	
	STRB	R0, [R1, 0x2B]						; Plain & Wood movement = 1 
	
	B		Set_Temp_Terrain_Defense
	
;-------------------------------------------------------------------------------

No_Skill_Pathfinder:

	LDR 	R1, [R5, 0]
	LDR 	R0, [R4, 0]
	ADD 	R0, R1, R0, LSL 2
	LDR 	R0, [R0, 0x48]
	ADD 	R0, R0, 0x5000	
	LDR 	R0, [R0, 0xA58]
	ADD		R1, R0, R8
	LDR		R0, [SP, 0xC]
	
	STRB	R0, [R1, 0x2B]						; Plain movement = 1 
	
	B		Set_Temp_Terrain_Defense	
	
;-------------------------------------------------------------------------------
	
No_Skill_Prairie_Dog:

	LDR 	R2, [R5, 0]
	LDR 	R0, [R4, 0]
	LDR 	R1, [SP, 4]
	
	ADD 	R0, R2, R0, LSL 2
	LDR 	R0, [R0, 0x48]
	ADD 	R0, R0, 0x5000	
	LDR 	R0, [R0, 0xA58]
	ADD		R0, R0, R8
	ADD		R0, R0, 0x74
	
	BL 		Check_Skills_Equipped				; Check the Skill is equipped
	CMP 	R0, 0								; Does CO use Pathfinder?
	BEQ		Set_Temp_Terrain_Defense			; Branch if CO DOES NOT use Pathfinder
	
	LDR 	R1, [R5, 0]
	LDR 	R0, [R4, 0]
	ADD 	R0, R1, R0, LSL 2
	LDR 	R0, [R0, 0x48]
	ADD 	R0, R0, 0x5000	
	LDR 	R0, [R0, 0xA58]
	ADD		R1, R0, R8
	LDR		R0, [SP, 0x10]
	
	STRB	R0, [R1, 0x2B]						; Wood movement = 1 
	
;-------------------------------------------------------------------------------

Set_Temp_Terrain_Defense:

	LDR 	R1, [R5, 0]
	LDR 	R0, [R4, 0]
	LDR 	R9, [SP, 0x14]
	ADD 	R0, R1, R0, LSL 2
	LDR 	R0, [R0, 0x48]
	LDR		R7, [SP, 0x18]
	ADD 	R0, R0, 0x5000	
	LDR 	R0, [R0, 0xA58]
	ADD		R0, R0, R8
	
	STRB	R11, [R0, 0x2A]						; The "Temporary Terrain Defence" is set to 0.
	
;-------------------------------------------------------------------------------

Movement_Cost_BLT_1:

	AND		R0, R9, 0xFF
	
	BL		0xC237C
	
	CMP 	R0, 0								; 
	BEQ		Movement_Cost_Break_3				; 
	
	LDR 	R1, [R5, 0]
	LDR 	R0, [R4, 0]
	ADD 	R0, R1, R0, LSL 2
	LDR 	R0, [R0, 0x48]
	ADD 	R0, R0, 0x5000	
	LDR 	R0, [R0, 0xA58]
	ADD		R3, R8, R0
	ADD		R12, R0, R7
	LDRB	R1, [R3, 0x2C]
	LDRB	R0, [R12, 0x2C]
	
	CMP 	R1, R0								; 
	BEQ		Movement_Cost_Break_3				; 
	
	LDRSB	R2, [R3, 0x2A]
	LDRB	R1, [R12, 0x6C]
	LDR		R0, [R12, 0x6C]
	MOV		R12, 0x220
	AND		R1, R1, 0x7F
	MLA		R12, R1, R12, R6
	MOV		R0, R0, LSL 0x14
	MOV		R0, R0, LSR 0x1E
	ADD		R0, R12, R0, LSL 0x7
	LDRSH	R1, [R0, 0xCA]
	MOV		R0, 10
	SMULBB	R0, R1, R0
	SUB 	R0, R2, R0
	
	STRB	R0, [R3, 0x2A]
	
Movement_Cost_Break_3:

	ADD		R7, R7, 0x98
	ADD		R9, R9, 1
	CMP		R9, 5
	
	BLT		Movement_Cost_BLT_1

Movement_Cost_Break_2:
	
	ADD		R10, R10, 1
	CMP		R10, 5
	ADD		R8, R8, 0x98
	
	BLT		Movement_Cost_BLT_2

	BL		0xC0AA8	
		
	ADD		SP, SP, 0x1C
	POP 	{R4-R11, LR}
	
	BX 		LR

;-------------------------------------------------------------------------------		

Movement_Cost_1:
	.dw 0x0215360C

Movement_Cost_2:
	.dw Reg1_Skill
	
Movement_Cost_3:
	.dw Reg2_Skill



